package com.citicpub_cloud.restfull.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.citicpub_cloud.restfull.api.dto.GoodsInfoDTO;
import com.citicpub_cloud.restfull.dao.core.TbGoodsInfo;
import com.citicpub_cloud.restfull.dao.jdbc.AbstractJdbcDaoSupport;

@Service
public class GoodsDaoService extends AbstractJdbcDaoSupport {
	public GoodsInfoDTO getGoodsDetailById(Long goodsId,String goodsNum,Integer gtype,Long resId,Long pid) {
		List<Object> objlist = new ArrayList<Object>();
		try {
			String sql = "select tgi.*,case when tgi.is_third_resource = 1 then (select tti.third_name from tb_third_info tti where tti.third_num = tgi.third_num) else '' end as third_name from tb_goods_info tgi where 1=1 ";
			
			if(null != goodsId){
				objlist.add(goodsId);
				sql += "and tgi.id = ? ";
			}
			if(!StringUtils.isBlank(goodsNum)){
				objlist.add(goodsNum);
				sql += "and tgi.goods_num = ? ";
			}
			if(null != gtype){
				objlist.add(gtype);
				sql += "and tgi.goods_type = ? ";
			}
			if(null != resId){
				objlist.add(resId);
				sql += "and tgi.res_id = ? ";
			}
			if(null != pid){
				objlist.add(pid);
				sql += "and tgi.b_platform_id = ? ";
			}
			
			RowMapper<TbGoodsInfo> mapper = new RowMapper<TbGoodsInfo>() {

				@Override
				public TbGoodsInfo mapRow(ResultSet rs, int index) throws SQLException {
					TbGoodsInfo tbGoodsInfo = new TbGoodsInfo();
					tbGoodsInfo.setExtInfo(rs.getString("ext_info"));
					tbGoodsInfo.setGoodsDesc(rs.getString("goods_desc"));
					tbGoodsInfo.setGoodsSummary(rs.getString("goods_summary"));
					tbGoodsInfo.setGoodsImg(rs.getString("goods_img"));
					tbGoodsInfo.setGoodsImgId(rs.getLong("goods_img_id"));
					tbGoodsInfo.setGoodsItemId(rs.getLong("goods_item_id"));
					tbGoodsInfo.setGoodsItemName(rs.getString("goods_item_name"));
					tbGoodsInfo.setGoodsKeyword(rs.getString("goods_key_word"));
					tbGoodsInfo.setGoodsName(rs.getString("goods_name"));
					tbGoodsInfo.setGoodsNum(rs.getString("goods_num"));
					tbGoodsInfo.setGoodsOnlineStatus(rs.getInt("goods_online_status"));
					tbGoodsInfo.setGoodsParentId(rs.getLong("goods_parent_id"));
					tbGoodsInfo.setGoodsPriceId(rs.getLong("goods_price_id"));
					tbGoodsInfo.setGoodsType(rs.getInt("goods_type"));
					tbGoodsInfo.setGoodsVerityStatus(rs.getInt("goods_verity_status"));
					tbGoodsInfo.setId(rs.getLong("id"));
					tbGoodsInfo.setIsDelete(rs.getInt("is_delete"));
					tbGoodsInfo.setResId(rs.getLong("res_id"));
					tbGoodsInfo.setResName(rs.getString("res_name"));
					tbGoodsInfo.setResSimpleDesc(rs.getString("res_simple_desc"));
					tbGoodsInfo.setResType(rs.getInt("res_type"));
					tbGoodsInfo.setResAuthor(("null" == rs.getString("res_author") || null == rs.getString("res_author"))?"":rs.getString("res_author"));
					tbGoodsInfo.setBookAuthor(("null" == rs.getString("book_author") || null == rs.getString("book_author"))?"":rs.getString("book_author"));
					tbGoodsInfo.setIsMainLandBuy(rs.getInt("is_mainland_buy"));
					tbGoodsInfo.setFreeChapter(rs.getInt("free_chapter"));
					tbGoodsInfo.setIsFree(rs.getInt("is_free"));
					tbGoodsInfo.setChannelType(rs.getInt("channel_type"));
					tbGoodsInfo.setIsFinished(rs.getByte("is_finished"));
					tbGoodsInfo.setFinishTime(rs.getString("finish_time"));
					tbGoodsInfo.setIsFreeChapter(rs.getByte("is_free_chapter"));
					tbGoodsInfo.setShowTime(rs.getString("show_time"));
					tbGoodsInfo.setGoodsHorizontalImg(rs.getString("goods_horizontal_img"));
					tbGoodsInfo.setGoodsHeadImg(rs.getString("goods_head_img"));
					tbGoodsInfo.setDescType(rs.getByte("desc_type"));
					tbGoodsInfo.setDescImg(rs.getString("desc_img"));
					tbGoodsInfo.setGoodsSubType(rs.getInt("goods_sub_type"));
					tbGoodsInfo.setIsEntityChapter(rs.getByte("is_entity_chapter"));
					tbGoodsInfo.setEntityChapterShowCount(rs.getInt("entity_chapter_show_count"));
					tbGoodsInfo.setGoodsSubname(rs.getString("goods_subname"));
					tbGoodsInfo.setGoodsDesc(rs.getString("goods_desc"));
					tbGoodsInfo.setBplatformId(rs.getLong("b_platform_id"));
					tbGoodsInfo.setIsThirdResource(rs.getByte("is_third_resource"));
					tbGoodsInfo.setThirdNum(rs.getString("third_num"));
					tbGoodsInfo.setThirdName(rs.getString("third_name"));
					tbGoodsInfo.setGoodsSquareImg(rs.getString("goods_square_img"));
					tbGoodsInfo.setBookTranslator(rs.getString("book_translator"));
					tbGoodsInfo.setEditorRecommendation(rs.getString("editor_recommendation"));
					tbGoodsInfo.setDoubanscoreView(rs.getByte("doubanscore_view"));
					tbGoodsInfo.setBgColor(rs.getString("bg_color"));
					tbGoodsInfo.setFontColorFlag(rs.getByte("font_color_flag"));
					tbGoodsInfo.setBookId(rs.getLong("book_id"));
					tbGoodsInfo.setContentAlbumId(rs.getLong("content_album_id"));
					return tbGoodsInfo;
				}
			};
			List<TbGoodsInfo> goodsList = jdbcTemplate.query(sql, objlist.toArray(), mapper);
			if (null != goodsList && goodsList.size() > 0) {
				TbGoodsInfo tbGoodsInfo = goodsList.get(0);
				GoodsInfoDTO goodsInfoDTO = new GoodsInfoDTO();
				BeanUtils.copyProperties(goodsInfoDTO, tbGoodsInfo);
				return goodsInfoDTO;
			}
		} catch (Exception e) {
			log.error("根据商品id查询商品详情信息出错", e);
		}
		return null;
	}
}
